﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using UI.Online;
using DatabaseHelper;

namespace UI.Online.SubForms
{
    public partial class frmManageProjects : Form
    {
        public frmManageProjects()
        {
            InitializeComponent();
            btnPipelines.Enabled = false;
            btnExport.Enabled = false;
            lvProjectList.Columns.Add("Project Name", 150, HorizontalAlignment.Left);
            lvProjectList.Columns.Add("Location", 150, HorizontalAlignment.Left);
            lvProjectList.Columns.Add("Summary", 150, HorizontalAlignment.Left);
        }

        private void frmAddProject_Load(object sender, EventArgs e)
        {
            CommonUseMethods cum = new CommonUseMethods();
            cum.LoadProjectList(ref lvProjectList);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmNewProject frm = new frmNewProject();
            frm.ShowDialog(); frm.Dispose();
            CommonUseMethods cum = new CommonUseMethods();
            cum.LoadProjectList(ref lvProjectList);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            DBHelper dh = new DBHelper();
            foreach (ListViewItem Item in lvProjectList.SelectedItems)
            {
                string str = Item.SubItems[0].Text;
                dh.Query = "DELETE * FROM Projects WHERE ProjectName = '" + str + "'";
                dh.Execute();
                lvProjectList.Items.Remove(Item);
            }
        }

        private void btnPipelines_Click(object sender, EventArgs e)
        {
            PProject pro = new PProject();
            pro.ProjectName = lvProjectList.SelectedItems[0].SubItems[0].Text;
            pro.Location = lvProjectList.SelectedItems[0].SubItems[1].Text;
            pro.Summary = lvProjectList.SelectedItems[0].SubItems[2].Text;
            frmPipelines frm = new frmPipelines(pro);
            frm.ShowDialog(); frm.Dispose();
        }

        private void lvProjectList_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnPipelines.Enabled = true;
            btnExport.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (lvProjectList.SelectedItems.Count == 1)
            {
                DBHelper dh = new DBHelper();
                string projectName = lvProjectList.SelectedItems[0].SubItems[0].Text;
                dh.Query = "SELECT * FROM Projects WHERE ProjectName = '" + projectName + "'";
                DataTable dt = dh.Fill();
                if (dt.Rows.Count == 1)
                {
                    int projectId = int.Parse(dt.Rows[0]["ProjectID"].ToString());
                    dh.ExportAllEventsToExcel(projectId);
                }
            }
        }
    }
}
